package org.fnzn.util;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.fnzn.core.Result;
import org.springframework.stereotype.Service;
import org.fnzn.config.WeixinProperties;
import org.fnzn.exception.MyException;

@Service
public class SendHttpService {

	private static  String url;   //与微信交互地址
	@Resource
	public  void setUrl(WeixinProperties weixinProperties) {
		SendHttpService.url = weixinProperties.getUrl();
	}
	public static Result sendHttp(Object obj) {
		String wxStr=JsonUtils.objectToJson(obj);
		String result=HttpUtil.doPostJson(url, wxStr);		
		if(StringUtils.isBlank(result)) {
			throw new MyException("向微信发送消息失败");
		}else {
			Result resultobj =null;
			try{
				resultobj=JsonUtils.jsonToPojo(result, Result.class);			
			}catch(Exception e){
				throw new MyException("与微信交互失败");
			}
			if(resultobj==null||resultobj.getCode()!=200) {
				throw new MyException("与微信交互失败");
			}
			return resultobj;
		}
	}
}
